package application1.servlet;

import common.action.SaveProductAction;
import common.domain.Product;
import common.form.ProductForm;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 控制器Servlet类
 * |- 实现HttpServlet类，并重写doGet、doPost等方法
 * |- 让重写的方法都调用自定义的process方法，用来统一处理调用
 * |- 使用WebServlet 注解后，就无需在web.xml文件中声明匹配路径了
 *
 */
@WebServlet(name = "ControllerServlet", urlPatterns = {"/product_input1.action", "/product_save1.action"})
public class ControllerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 调用本类中的方法
        process(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 调用本类中的方法
        process(req, resp);
    }

    /**
     * 自定义的处理方法，用来统一处理不同类型的调用
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        // 1、获取URI路径
        String uri = request.getRequestURI();
        request.setCharacterEncoding("UTF-8");// 解决中文乱码

        // 2、处理路径，获取相应的action
        int lastIndex = uri.lastIndexOf("/");
        String action = uri.substring(lastIndex + 1);

        // 3、进行路径匹配，控制器类的核心
        if (action.equals("product_input1.action")) {
            // 不用做什么
        } else if (action.equals("product_save1.action")) {
            /**
             * 1. 将表单对象转化成领域对象
             * 2. 处理业务逻辑
             * 3. 将领域对象存储到HttpSession中
             */
            ProductForm productForm = new ProductForm();
            productForm.setName(request.getParameter("name"));
            productForm.setDescription(request.getParameter("description"));
            productForm.setPrice(request.getParameter("price"));

            Product product = new Product();
            product.setName(productForm.getName());
            product.setDescription(productForm.getDescription());
            try {
                product.setPrice(Float.parseFloat(productForm.getPrice()));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            // 业务操作
            SaveProductAction saveProductAction = new SaveProductAction();
            saveProductAction.save(product);

            // 存储HttpSession中
            request.setAttribute("product", product);
        }

        // 4、进行视图跳转
        String dispatcherUrl = null;
        if (action.equals("product_input1.action")) {
            dispatcherUrl = "/WEB-INF/application1/jsp/ProductForm1.jsp";
        } else if (action.equals("product_save1.action")) {
            dispatcherUrl = "/WEB-INF/application1/jsp/ProductDetail1.jsp";
        }
        if (dispatcherUrl != null) {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher(dispatcherUrl);
            requestDispatcher.forward(request, response);
        }

    }


}
























